﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SmartMathLibrary
{
    /// <summary>
    /// The class Point3DCollection represents a collection of points.
    /// </summary>
    [Serializable]
    public class Point3DCollection : AbstractCollection
    {
        /// <summary>
        /// This list of points.
        /// </summary>
        private readonly List<Point3D> list;

        /// <summary>
        /// Initializes a new instance of the <see cref="Point3DCollection"/> class.
        /// </summary>
        public Point3DCollection()
        {
            this.list = new List<Point3D>();
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="Point3DCollection"/> class.
        /// </summary>
        /// <param name="collection">The collection to clone.</param>
        public Point3DCollection(Point3DCollection collection)
        {
            if (collection == (Point3DCollection) null)
            {
                throw new ArgumentNullException("collection");
            }

            this.list = collection.Value;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="Point3DCollection"/> class.
        /// </summary>
        /// <param name="list">The list which includes the data for the collection.</param>
        public Point3DCollection(List<Point3D> list)
        {
            if (list == (List<Point3D>) null)
            {
                throw new ArgumentNullException("list");
            }

            this.list = list;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="Point3DCollection"/> class.
        /// </summary>
        /// <param name="list">An array which includes the data for the collection.</param>
        public Point3DCollection(Point3D[] list)
        {
            if (list == (Point3D[]) null)
            {
                throw new ArgumentNullException("list");
            }

            if (list.Length != 0)
            {
                throw new ArgumentException("list.Length != 0", "list");
            }

            foreach (Point3D p in list)
            {
                this.list.Add(p);
            }
        }

        /// <summary>
        /// Gets the value of the collection.
        /// </summary>
        /// <value>The value of the collection.</value>
        public List<Point3D> Value
        {
            get { return list; }
        }

        /// <summary>
        /// Gets the number of items of the collection.
        /// </summary>
        /// <value>The number of items of the collection.</value>
        public int Count
        {
            get { return list.Count; }
        }

        /// <summary>
        /// Sorts the points, which the collection includes, by their x values.
        /// </summary>
        public void Sort()
        {
            this.list.Sort();
        }

        /// <summary>
        /// Copies the current instance.
        /// </summary>
        /// <returns>A copy of the current instance.</returns>
        public Point3DCollection Copy()
        {
            return new Point3DCollection(this.ToArray());
        }

        /// <summary>
        /// Adds a point to the current collection.
        /// </summary>
        /// <param name="point">The point to add.</param>
        public void Add(Point3D point)
        {
            this.list.Add(point);
        }

        /// <summary>
        /// Inserts an element into the current collection at a specified position.
        /// </summary>
        /// <param name="index">The specified insert position.</param>
        /// <param name="point">The point to insert.</param>
        public void Insert(int index, Point3D point)
        {
            this.list.Insert(index, point);
        }

        /// <summary>
        /// Removes all items from the current collection.
        /// </summary>
        public void Clear()
        {
            this.list.Clear();
        }

        /// <summary>
        /// Removes an element at a specified position.
        /// </summary>
        /// <param name="index">The specified position.</param>
        public void RemoveAt(int index)
        {
            this.list.RemoveAt(index);
        }

        /// <summary>
        /// Gets the element at a specified position.
        /// </summary>
        /// <param name="index">The specified position.</param>
        /// <returns>The element at a specified position.</returns>
        public Point3D GetElementAt(int index)
        {
            return this.list[index];
        }

        /// <summary>
        /// Checks if the specified point exists in the collection.
        /// </summary>
        /// <param name="check">The point with the values to check.</param>
        /// <returns>True if the point exists in the collection otherwise, false.</returns>
        public bool Exist(Point3D check)
        {
            foreach (Point3D p in this.list)
            {
                if (p == check)
                {
                    return true;
                }
            }

            return false;
        }

        /// <summary>
        /// Gets the minimum value in the collection of points. If the collection is empty 
        /// the method will return a point with zero elements (0; 0).
        /// </summary>
        /// <returns>The minimum value in the collection of points.</returns>
        public Point3D GetMinimum()
        {
            if (this.list.Count >= 1)
            {
                Point3D check = this.list[0];

                for (int i = 1; i < this.list.Count; i++)
                {
                    if (this.list[i] < check)
                    {
                        check = this.list[i];
                    }
                }

                return check;
            }

            return new Point3D();
        }

        /// <summary>
        /// Gets the maximum value in the collection of points. If the collection is empty 
        /// the method will return a point with zero elements (0; 0; 0).
        /// </summary>
        /// <returns>The maximum value in the collection of points.</returns>
        public Point3D GetMaximum()
        {
            if (this.list.Count >= 1)
            {
                Point3D check = this.list[0];

                for (int i = 1; i < this.list.Count; i++)
                {
                    if (this.list[i] > check)
                    {
                        check = this.list[i];
                    }
                }

                return check;
            }

            return new Point3D();
        }

        /// <summary>
        /// Compares the current instance to another.
        /// </summary>
        /// <param name="compare">The collection to compare.</param>
        /// <returns>True if the collection values and the order of the items are even otherwise, false.</returns>
        public bool CompareTo(Point3DCollection compare)
        {
            return this == compare;
        }

        /// <summary>
        /// Implements the operator ==.
        /// </summary>
        /// <param name="a">The first Point3DCollection.</param>
        /// <param name="b">The second Point3DCollection.</param>
        /// <returns>The result of the operator.</returns>
        public static bool operator ==(Point3DCollection a, Point3DCollection b)
        {
            if (System.Object.ReferenceEquals(a, b))
            {
                return true;
            }

            if (((object) a == null) || ((object) b == null))
            {
                return false;
            }

            for (int i = 0; i < a.Count; i++)
            {
                if (a.GetElementAt(i) != b.GetElementAt(i))
                {
                    return false;
                }
            }

            return true;
        }

        /// <summary>
        /// Implements the operator !=.
        /// </summary>
        /// <param name="a">The first Point3DCollection.</param>
        /// <param name="b">The second Point3DCollection.</param>
        /// <returns>The result of the operator.</returns>
        public static bool operator !=(Point3DCollection a, Point3DCollection b)
        {
            return !(a == b);
        }

        /// <summary>
        /// Gets or sets the <see cref="SmartMathLibrary.Point3D"/> at the specified index.
        /// </summary>
        /// <value>The Point3D value.</value>
        public Point3D this[int index]
        {
            get { return this.list[index]; }
            set { this.list[index] = value; }
        }

        /// <summary>
        /// Gets the enumerator.
        /// </summary>
        /// <returns>The enumerator of the current iteration.</returns>
        public IEnumerator<Point3D> GetEnumerator()
        {
            for (int i = 0; i < this.list.Count; i++)
            {
                yield return this.list[i];
            }
        }

        /// <summary>
        /// Determines whether the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>.
        /// </summary>
        /// <param name="obj">The <see cref="T:System.Object"/> to compare with the current <see cref="T:System.Object"/>.</param>
        /// <returns>
        /// true if the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>; otherwise, false.
        /// </returns>
        /// <exception cref="T:System.NullReferenceException">The <paramref name="obj"/> parameter is null.</exception>
        public override bool Equals(object obj)
        {
            return base.Equals(obj);
        }

        /// <summary>
        /// Serves as a hash function for a particular type.
        /// </summary>
        /// <returns>
        /// A hash code for the current <see cref="T:System.Object"/>.
        /// </returns>
        public override int GetHashCode()
        {
            return base.GetHashCode();
        }

        /// <summary>
        /// Converts the current collection to an array of points.
        /// </summary>
        /// <returns>The converted point array.</returns>
        public Point3D[] ToArray()
        {
            return this.list.ToArray();
        }

        /// <summary>
        /// Returns a <see cref="T:System.String"/> that represents the current <see cref="T:System.Object"/>.
        /// </summary>
        /// <returns>
        /// A <see cref="T:System.String"/> that represents the current <see cref="T:System.Object"/>.
        /// </returns>
        public override string ToString()
        {
            String result = "";

            foreach (Point3D p in this.list)
            {
                result += "(" + p.ToString() + ") ";
            }

            return base.ToString().Trim();
        }
    }
}